package com.pospi.pai.yunpos.diaob;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.lany.sp.SPHelper;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.mylhyl.circledialog.view.listener.OnCreateBodyViewListener;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.http.HttpConnection;
import com.pospi.modle.GoodsBeen;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.been.BillgoodsDetailAdapter;
import com.pospi.pai.yunpos.been.SaveOrderInfo;
import com.pospi.pai.yunpos.been.SupBeen;
import com.pospi.pai.yunpos.caigou.ReceiveDetailActivity;
import com.pospi.pai.yunpos.caigou.StatusBeen;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.pai.yunpos.util.BillUtil;
import com.pospi.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lib.kingja.switchbutton.SwitchMultiButton;

/*
*调拨收货
 */

public class DboChaYListActivity extends Base2Activity {
    @Bind(R.id.total_je)
    TextView total_je;
    @Bind(R.id.bill_num)
    TextView bill_num;
    @Bind(R.id.switch_button)
    SwitchMultiButton switch_button;
    @Bind(R.id.tv_list_title)
    TextView tv_list_title;
//    @Bind(R.id.iv_search_counts)
//    ImageView iv_search_counts;
    @Bind(R.id.bill_num_title)
    LinearLayout bill_num_title;
    @Bind(R.id.djsl_title)
    TextView djsl_title;

    @Bind(R.id.md_title)
    TextView md_title;
    @Bind(R.id.gys_title)
    TextView gys_title;
    @Bind(R.id.gys_ll)
    LinearLayout gys_ll;
    @Bind(R.id.time_start_select)
    TextView time_start_select;
    @Bind(R.id.time_end_select)
    TextView time_end_select;
    @Bind(R.id.djbh)
    EditText bill_no;
    @Bind(R.id.cgmd)
    TextView tv_shop;
    @Bind(R.id.gys)
    TextView tv_sup;
    @Bind(R.id.status_bill)
    TextView status_bill;

    private List<BillBeen> beens = new ArrayList<>();
    private String count;
    private String je;
    private MyAdapter adapter;


    private String market = "";
    private String status = "";
    private String supid = "";
    private BillgoodsDetailAdapter detailAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_db_cy);
        ButterKnife.bind(this);
        setTitle("调拨差异单");

        md_title.setText("调出门店");
        gys_title.setText("调入门店");
        time_start_select.setText(GetData.getYYMMDDTime());
        time_end_select.setText(GetData.getYYMMDDTime());
        init();
        downStore();
        downSup();
        downCus();
    }




    private void init() {
        StatusBeen been1 = new StatusBeen();
        been1.setId("2");
        been1.setName("未收货");
        StatusBeen been2 = new StatusBeen();
        been2.setId("3");
        been2.setName("已收货");
        statusBeens.add(been2);
        statusBeens.add(been1);
//        iv_search_counts.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(DboReceiveListActivity.this, DboSearchActivity.class);
//                intent.putExtra("mark", "rk");
//                startActivity(intent);
//            }
//        });
        switch_button.setText("今天","昨天","近三天","所有");
        switch_button.setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() {
            @Override
            public void onSwitch(int position, String tabText) {
                switch (position) {
                    case 0:
                        startrq = GetData.getAnyTime(0);
                        endrq = GetData.getAnyTime(0);
//                        searchBillfast();
                        break;
                    case 1:
                        startrq = GetData.getAnyTime(1);
                        endrq = GetData.getAnyTime(1);
//                        searchBillfast();
                        break;
                    case 2:
                        startrq = GetData.getAnyTime(3);
                        endrq = GetData.getAnyTime(0);
//                        searchBillfast();
                        break;
                    case 3:
                        searchBill();
                        break;
                }
            }
        });
        tv_list_title.setText("没有查询到数据");

        ListView bill_list = (ListView) findViewById(R.id.list_bill);
        adapter = new MyAdapter();
        bill_list.setAdapter(adapter);
        bill_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                getbillDialog(beens.get(i));
//                Intent intent = new Intent(DboChaYListActivity.this, DboDetailActivity.class);
//                intent.putExtra("data", beens.get(i));
//                startActivity(intent);
            }
        });
//        searchBillfast();

    }


    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return beens.size();
        }

        @Override
        public Object getItem(int i) {
            return beens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder ;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(DboChaYListActivity.this).inflate(R.layout.cgdd_list_item, null);
                holder.statusname = view.findViewById(R.id.statusname);
                holder.billno = view.findViewById(R.id.billno);
                holder.je = view.findViewById(R.id.je);
                holder.billrq = view.findViewById(R.id.billrq);
                holder.marketname = view.findViewById(R.id.marketname);
                holder.supname = view.findViewById(R.id.supname);
                holder.tv_xh = view.findViewById(R.id.tv_xh);
                holder.ll_md = view.findViewById(R.id.ll_md);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.ll_md.setVisibility(View.GONE);
            BillBeen cgddBeen = beens.get(i);
            holder.supname.setText(cgddBeen.getOutmarketname()+" --> "+cgddBeen.getInmarketname());
            holder.statusname.setImageResource(cgddBeen.getStatus().equals("1") ? R.drawable.icon_chart_wsh : R.drawable.icon_chart_ysh);
            holder.billrq.setText(cgddBeen.getBillrq());
            holder.billno.setText(cgddBeen.getBillno());
            holder.je.setText(cgddBeen.getJe());
            holder.tv_xh.setText(String.valueOf(i+1));
            return view;
        }
        class Holder{
            ImageView statusname;
            TextView billno;
            TextView je;
            TextView billrq;
            TextView marketname;
            TextView supname;
            TextView tv_xh;
            LinearLayout ll_md;
        }
    }
    private void searchBill() {
        showDialog();
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "bill.mbilldb");
        params.put("type", "998");
        params.put("type1", "998");
        params.put("fun", "find_list");

        params.put("page", "1");
        params.put("limit", "20");


        JSONObject object = new JSONObject();
        try {
//            object.put("startrq", "");
//            object.put("endrq", "");
//            object.put("billno", "");
//            object.put("outmarket", "");
//            object.put("inmarket", "");
//            object.put("num", "");
//            object.put("barcode", "");
//            object.put("status", "2");
            object.put("filter", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("login",params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    Log.i("login", jsonObject.getString("errMsg"));
                    List<BillBeen> cgddBeens = new Gson().fromJson(result.toString(),
                            new TypeToken<List<BillBeen>>() {
                            }.getType());
                    beens.clear();
                    beens.addAll(cgddBeens);
                    JSONObject sumrow = jsonObject.getJSONObject("sumrow");
                    count = sumrow.getString("count");
                    je = sumrow.getString("je");

                } catch (JSONException e) {

                    e.printStackTrace();
                }
                Message message = Message.obtain();
                message.what = 1;
                handler.sendMessage(message);
            }
        });

    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    close();
                    if (beens.size() == 0) {
                        setbill(false);
                        adapter.notifyDataSetChanged();
                    } else {
                        setbill(true);
                        adapter.notifyDataSetChanged();
                    }

                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
            }

        }
    };
    private void setbill(boolean o){
        if (o) {
            bill_num_title.setVisibility(View.VISIBLE);
            tv_list_title.setText("总合计金额：");
            total_je.setText(je);
            bill_num.setText("" + count );
        } else {
            tv_list_title.setText("没有查询到数据");
            total_je.setText("");
            bill_num.setText("");
            bill_num_title.setVisibility(View.INVISIBLE);
            beens.clear();
            adapter.notifyDataSetChanged();
        }

    }

    private List<SupBeen> md_Beens = new ArrayList<>();//门店
    private List<StatusBeen> statusBeens = new ArrayList<>();

    //下载门店
    private void downStore() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastmarket");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    md_Beens = new Gson().fromJson(result.toString(), new TypeToken<List<SupBeen>>() {
                    }.getType());

                    md_Beens.add(new BillUtil().getNewSup());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });

    }
    //下载供应商
    private void downSup() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getsupplier");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.SUP_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }
    //下载客员工
    private void downCus() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getperson");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.CUS_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }
        });


    }

    private String startrq = GetData.getAnyTime(0);
    private String endrq = GetData.getAnyTime(0);
//    private void searchBillfast() {
//        showDialog();
//        Map<String,String> params = new HashMap<>();
//        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
//        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
//        params.put("model", "bill.Mbilldb");
//        params.put("type", "107");
//        params.put("fun", "find_list");
//
////        params.put("page", "");
////        params.put("limit", "");
//
//
//        JSONObject object = new JSONObject();
//        try {
//            object.put("startrq", startrq);
//            object.put("endrq", endrq);
//            object.put("billno", bill_no.getText().toString().trim());
//            object.put("outmarket", market);
//            object.put("inmarket", supid);
//            object.put("num", "");
//            object.put("barcode", "");
//            object.put("status", "2");//"1"="未审核""2"="发货审核" "3"="收货审核" "4"="作废"
////            object.put("filter", "");
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        params.put("pds", object.toString());
//        Log.i("login",params.toString());
//        new HttpConnection().postNet(params, new HttpCallBackListener() {
//            @Override
//            public void CallBack(String Response) {
//                Log.i("login", Response);
//                try {
//                    JSONObject jsonObject = new JSONObject(Response);
//                    JSONArray result = jsonObject.getJSONArray("result");
//                    Log.i("login", jsonObject.getString("errMsg"));
//                    List<BillBeen> cgddBeens = new Gson().fromJson(result.toString(),
//                            new TypeToken<List<BillBeen>>() {
//                            }.getType());
//                    beens.clear();
//                    beens.addAll(cgddBeens);
//                    JSONObject sumrow = jsonObject.getJSONObject("sumrow");
//                    count = sumrow.getString("count");
//                    je = sumrow.getString("je");
//                    Message message = Message.obtain();
//                    message.what = 1;
//                    handler.sendMessage(message);
//                } catch (JSONException e) {
//
//                    e.printStackTrace();
//                }
//            }
//        });
//
//    }
    private DialogFragment dialog;
    private void showDialog(){
        dialog = new CircleDialog.Builder()
                .setProgressText("加载中...")
                .setProgressStyle(ProgressParams.STYLE_SPINNER)
                .show(getSupportFragmentManager());
    }
    private void close(){
        dialog.dismiss();
    }

    private void showmd(View v) {
        List<String> strings = new ArrayList<>();
        strings.clear();
        for (SupBeen been : md_Beens) {
            strings.add(been.getName());
        }
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_yyy, null);
        final PopupWindow yyyPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        yyyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        yyyPopupWindow.setFocusable(true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = contentView.getMeasuredHeight();
        int width = contentView.getMeasuredWidth();
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        yyyPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - width / 2, location[1]);
        yyyPopupWindow.showAsDropDown(v);

        ListView listView = contentView.findViewById(R.id.yyy_list);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_shop.setText(md_Beens.get(position).getName());
                market = md_Beens.get(position).getId();
                yyyPopupWindow.dismiss();
            }
        });
    }
    private void showmd2(View v) {
        List<String> strings = new ArrayList<>();
        strings.clear();
        for (SupBeen been : md_Beens) {
            strings.add(been.getName());
        }
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_yyy, null);
        final PopupWindow yyyPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        yyyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        yyyPopupWindow.setFocusable(true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = contentView.getMeasuredHeight();
        int width = contentView.getMeasuredWidth();
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        yyyPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - width / 2, location[1]);
        yyyPopupWindow.showAsDropDown(v);

        ListView listView = contentView.findViewById(R.id.yyy_list);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_sup.setText(md_Beens.get(position).getName());
                supid = md_Beens.get(position).getId();
                yyyPopupWindow.dismiss();
            }
        });
    }

    private void showstatus(View v) {
        List<String> strings = new ArrayList<>();
        strings.clear();
        for (StatusBeen been : statusBeens) {
            strings.add(been.getName());
        }
        View contentView =  LayoutInflater.from(this).inflate(R.layout.dialog_yyy, null);
        final PopupWindow yyyPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        yyyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        yyyPopupWindow.setFocusable(true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = contentView.getMeasuredHeight();
        int width = contentView.getMeasuredWidth();
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        yyyPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - width / 2, location[1]);
        yyyPopupWindow.showAsDropDown(v);

        ListView listView = contentView.findViewById(R.id.yyy_list);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                status_bill.setText(statusBeens.get(position).getName());
                status = statusBeens.get(position).getId();
                yyyPopupWindow.dismiss();
            }
        });
    }

    @OnClick({R.id.cgmd, R.id.gys, R.id.status_bill, R.id.bt_search, R.id.time_start_select, R.id.time_end_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cgmd:
                showmd(view);
                break;
            case R.id.gys:
                showmd2(view);
                break;
            case R.id.status_bill:
                showstatus(view);
                break;
            case R.id.bt_search:
                searchBill();
                break;
            case R.id.time_start_select:
                TimePickerDialog dialogYearMonthDay = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setThemeColor(getResources().getColor(R.color.blue_cannot_click))
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                Date date = new Date(millseconds);
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                String time = format.format(date);
                                time_start_select.setText(time);
                                startrq = time;
                            }
                        })
                        .build();
                dialogYearMonthDay.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;
            case R.id.time_end_select:
                TimePickerDialog dialogYearMonthDay2 = new TimePickerDialog.Builder()
                        .setType(Type.YEAR_MONTH_DAY)
                        .setTitleStringId("选择日期")
                        .setWheelItemTextSize(16)
                        .setThemeColor(getResources().getColor(R.color.blue_cannot_click))
                        .setCallBack(new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                Date date = new Date(millseconds);
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                String time = format.format(date);
                                time_end_select.setText(time);
                                endrq = time;
                            }
                        })
                        .build();
                dialogYearMonthDay2.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;
//            case R.id.bt_threeday:
//
//                break;
        }
    }
    private DialogFragment restFragment;
    private List<GoodsBeen> beenList = new ArrayList<>();
    private void getbillDialog(final BillBeen been) {
        restFragment = new CircleDialog.Builder(this)
                .setBodyView(R.layout.dialog_dbcy, new OnCreateBodyViewListener() {
                    @Override
                    public void onCreateBodyView(View view) {
                        ListView listView = view.findViewById(R.id.list_bill);
                        detailAdapter = new BillgoodsDetailAdapter(DboChaYListActivity.this, beenList);
                        listView.setAdapter(detailAdapter);
                        getDetail(been);

                        TextView tv_out = view.findViewById(R.id.tv_out);
                        TextView tv_in = view.findViewById(R.id.tv_in);
                        TextView tv_md = view.findViewById(R.id.tv_md);
                        TextView tv_money = view.findViewById(R.id.tv_money_xz);
                        TextView sh_xz = view.findViewById(R.id.sh_xz);
                        TextView save_xz = view.findViewById(R.id.save_xz);
                        ImageView iv_close = view.findViewById(R.id.iv_close);
                        iv_close.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                closeBillDalog();
                            }
                        });

                        tv_out.setText("调出门店："+been.getOutmarketname());
                        tv_in.setText("调入门店："+been.getInmarketname());
                        tv_md.setText("日期：" + been.getBillrq());
                        tv_money.setText(been.getLsjje());
                        if (been.getStatus().equals("1")) {
                            sh_xz.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {//不同意
                                    disAgree(been);
                                }
                            });
                            save_xz.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {//同意
                                    confirm(been);
                                }
                            });
                        } else {
                            sh_xz.setVisibility(View.GONE);
                            save_xz.setVisibility(View.GONE);

                        }

                    }
                })
                .setWidth(0.8f)
                .show();
    }


    private void getDetail(BillBeen been) {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "bill.mbilldb");
        params.put("fun", "find_detail");
        params.put("type", "998");
        JSONObject object = new JSONObject();
        try {
            object.put("id", been.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONObject result = jsonObject.getJSONObject("result");
                    saveGoods(result);
                } catch (JSONException e) {

                }

            }
        });
    }

    private void confirm(BillBeen been) {
        showLodingDialog("请求中...");
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "bill.mbilldb");
        params.put("fun", "billsure");
        JSONObject object = new JSONObject();
        try {
            object.put("outmarket",been.getOutmarket() );
            object.put("inmarket", been.getInmarket());
            object.put("billrq", been.getBillrq());
            object.put("memo", "");
            object.put("person1", "");
            object.put("optname", "");
            object.put("optdt", "");
            object.put("billno", been.getBillno());
            object.put("status", 1);
            object.put("id", been.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    closeLodindDialog();
                    JSONObject jsonObject = new JSONObject(Response);
                    final String msg = jsonObject.getString("errMsg");
                    String code = jsonObject.getString("errCode");
                    if (code.equals("100")) {
                        closeBillDalog();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                new AlertDialog.Builder(DboChaYListActivity.this)
                                        .setTitle("提示")
                                        .setMessage("操作成功")
                                        .setPositiveButton("确定", null)
                                        .show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(DboChaYListActivity.this, msg, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DboChaYListActivity.this, "解析异常", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
    }
    private void disAgree(BillBeen been) {
        showLodingDialog("请求中...");
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "bill.mbilldb");
        params.put("fun", "billsurenot");
        params.put("table", "bill_dbhead");
        JSONObject object = new JSONObject();
        try {
            object.put("outmarket",been.getOutmarket() );
            object.put("inmarket", been.getInmarket());
            object.put("billrq", been.getBillrq());
            object.put("memo", "");
            object.put("person1", "");
            object.put("optname", "");
            object.put("optdt", "");
            object.put("billno", been.getBillno());
            object.put("status", 1);
            object.put("id", been.getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    closeLodindDialog();
                    JSONObject jsonObject = new JSONObject(Response);
                    final String msg = jsonObject.getString("errMsg");
                    String code = jsonObject.getString("errCode");
                    if (code.equals("100")) {
                        closeBillDalog();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                new AlertDialog.Builder(DboChaYListActivity.this)
                                        .setTitle("提示")
                                        .setMessage("操作成功")
                                        .setPositiveButton("确定", null)
                                        .show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(DboChaYListActivity.this, msg, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(DboChaYListActivity.this, "解析异常", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
    }

    private void closeBillDalog(){
        restFragment.dismiss();
    }
    private void saveGoods(JSONObject response) {
        List<GoodsBeen> list = new ArrayList<>();
        try {
            JSONArray array = response.getJSONArray("detail");
            list = SaveOrderInfo.changegoodsToList(array.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        beenList.clear();
        beenList.addAll(list);
        detailAdapter.notifyDataSetChanged();
    }
}
